<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Pagina_Inicial extends CI_Controller {

	/* Layout utilizado pela pagina. */
	public $layout = '';
	
	/* Titulo da Pagina. */
	public $title = CLIENTE;
	
	/* Menu do sistema*/
	public $menuTopo = '';
	
	/* Menu do sistema*/
	public $menuLateral = '';
	
	/* css utilizado pela pagina. */
	public $css = array();
	
	/* Js utilizado pela pagina. */
	public $js = array();
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('Config_Site_Manutencao_Model', 'manutencao');
		$this->load->model('Config_Email_Model', 'config_email');
	}
	
	public function index()	{					
		try {
			
			$configManu = $this->manutencao->obter();
			
			if (!empty($configManu) && $configManu->indManutencao === 'S'){
				
				$this->layout = "default_manutencao";
				
				array_push($this->js, 'manutencao/jquery-1.11.1', 'manutencao/lodash.min', 'manutencao/jquery.countdown');
				array_push($this->css, 'manutencao/style', 'manutencao/ie');
				
				$data = null;
				$data['dataManutencao'] = mostraDataManutencao($configManu->dtFimManutencao);
				
				$this->load->view('manutencao/manutencao', $data);
				
			}else{
				$this->layout = "default_site";
				
				array_push($this->js, 'plugins/jquery/jquery-2.1.4', 'site/bootstrap', 'site/pace.min', 'plugins/wow/wow.min', 'plugins/jquery-mask/jquery.mask.min', 'plugins/formValidation/formValidation', 'plugins/framework/bootstrap', 'plugins/classie/classie', 'site/cbpAnimatedHeader', 'site/site', 'plugins/waitMe/waitMe');
				array_push($this->css, 'site/bootstrap', 'site/animate.min', 'plugins/formValidation/formValidation', 'site/style', 'plugins/waitMe/waitMe');
				
				$this->load->view('site/site');
			}
			
		}catch (Exception $e){
			echo ($e->getMessage());
		}
		
	}
	
	public function enviaEmail()	{
		try {

			$this->layout = "";

			$configEmail = $this->config_email->obter();
			
			if (!empty($configEmail)){
				//Pegando dados do form
				$nome = trim($this->input->post('nome'));
				$assunto = trim($this->input->post('assunto'));
				$email = trim($this->input->post('email'));
				$telefone = trim($this->input->post('telefone'));
				$mensagem = trim($this->input->post('mensagem'));
				
				$config = Array (
						'protocol' => $configEmail->protocol,
						'smtp_host' => $configEmail->smtpHost,
						'smtp_port' => (int)$configEmail->smtpPort,
						'smtp_user' => $configEmail->smtpUser,
						'smtp_pass' => $configEmail->smtpPass,
						'smtp_timeout' => (int)$configEmail->smtpTimeout,
						'mailtype' => $configEmail->mailtype,
						'charset' => $configEmail->charset
				);
					
				$this->load->library ( 'email', $config );
				$this->email->set_newline ( "\r\n" );
					
				$this->email->from ( $configEmail->fromEmail, $configEmail->fromDescricao );
				$this->email->to ( $email );
					
				$assunto = "Fale Conosco";
				$this->email->subject ( $assunto );
					
				$corpo = 'Você recebeu um email através do Fale Conosco <br><br><br>
						  <strong>Nome: </strong> '.$nome.'<br>
						  <strong>Assunto: </strong> '.$assunto.'<br>
						  <strong>Email: </strong> '.$email.'<br>
						  <strong>Telefone: </strong> '.$telefone.'<br>
						  <strong>Mensagem: </strong> '.$mensagem.'<br>';
					
				$this->email->message ( $corpo );
					
				$retorno = $this->email->send (FALSE);
					
				if ((int)$retorno === 1) {
					echo true;
				} else {
					echo $this->email->print_debugger();
				}
			}else{
				echo false;
			}
			
		}catch (Exception $e){
			echo ($e->getMessage());
		}
	
	}
	
}